package br.com.site.actions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import br.com.site.model.dao.DAOException;
import br.com.site.model.entity.Estande;
import br.com.site.model.entity.Usuario;

@ParentPackage(value = "default")
public class ManterColaboradores extends Listagem<Usuario> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Long idEstande;

	private List<String> idsColaboradores;

	@Action(value = "/listarColaboradores", results = {
			@Result(location = "listarColaboadores.jsp", name = SUCCESS),
			@Result(location = "loginsucess.jsp", name = BLOQUEAR),
			@Result(type = "redirectAction", location = "error", name = ERROR),
			@Result(type = "redirectAction", location = "login", name = LOGIN) })
	public String execute() throws Exception {
		clearErrorsAndMessages();
		if (verAcesso()) {

			try {
				init();
				nextPage();

			} catch (DAOException e) {

				/*
				 * addActionError(e.getMessage()); return ERROR;
				 */
				String error = "***ListarMural*** <br>" + e.toString();
				getSessao().put("myerror", error);
				return ERROR;
			}

			return SUCCESS;

		}
		addActionError("Acesso Negado!");
		return BLOQUEAR;
	}

	@Action(value = "/alterarColaboradores", results = {
			@Result(location = "listarColaboadores.jsp", name = SUCCESS),
			@Result(location = "loginsucess.jsp", name = BLOQUEAR),
			@Result(type = "redirectAction", location = "error", name = ERROR),
			@Result(type = "redirectAction", location = "login", name = LOGIN) })
	public String saveColaboradores() throws Exception {
		try {
			Estande estande = Estande.find(idEstande);
			estande.getColaboradores()
					.removeAll(
							Usuario.find(getPageatual() * getPagesize(),
									getPagesize()));
			if (idsColaboradores != null) {
				for (String login : idsColaboradores) {
					Usuario userTemp = new Usuario();
					userTemp.setLogin(login);
					if (!estande.getColaboradores().contains(userTemp)) {
						estande.getColaboradores().add(userTemp);
					}
				}
			}
			estande.save();
			addActionMessage("Lista de Colaboradores alterada com sucesso.");
		} catch (Exception e) {
			String error = "***ListarMural*** <br>" + e.toString();
			getSessao().put("myerror", error);
			return ERROR;
		}
		super.setPageatual(getPageatual() - 1);
		return super.nextPage();
	}

	private void init() {
		this.setPageatual(-1);
	}

	@Override
	public List<Usuario> listar() throws Exception {
		// Preenchendo a lista de colaboradores para ser usada na view
		if (idsColaboradores == null) {
			idsColaboradores = new ArrayList<String>();
			Estande estande = Estande.find(idEstande);
			if (estande != null) {
				List<Usuario> colaboradores = estande.getColaboradores();
				Iterator<Usuario> it = colaboradores.iterator();
				while (it.hasNext()) {
					idsColaboradores.add(it.next().getId());
				}
			}
		}
		return Usuario.find(getPageatual() * getPagesize(), getPagesize());
	}

	public Long getIdEstande() {
		return idEstande;
	}

	public void setIdEstande(Long idEstande) {
		this.idEstande = idEstande;
	}

	public List<String> getIdsColaboradores() {
		return idsColaboradores;
	}

	public void setIdsColaboradores(List<String> idsColaboradores) {
		this.idsColaboradores = idsColaboradores;
	}

}
